package view;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;

public class LoginView extends JFrame {
    private final JTextField usernameField;
    private final JPasswordField passwordField;
    private final JButton loginButton;
    private final JComboBox<String> roleComboBox;
    private final JButton registerButton;

    public LoginView() {
        setTitle("视频播放系统登录");
        setSize(350, 250);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());

        // 主输入面板（使用GridBagLayout布局）
        JPanel inputPanel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(5, 5, 5, 5);
        gbc.fill = GridBagConstraints.HORIZONTAL;

        // 角色选择
        gbc.gridx = 0; gbc.gridy = 0;
        inputPanel.add(new JLabel("角色:"), gbc);
        gbc.gridx = 1;
        roleComboBox = new JComboBox<>(new String[]{"用户", "管理员"});
        inputPanel.add(roleComboBox, gbc);

        // 用户名
        gbc.gridx = 0; gbc.gridy = 1;
        inputPanel.add(new JLabel("用户名:"), gbc);
        gbc.gridx = 1;
        usernameField = new JTextField(15);
        inputPanel.add(usernameField, gbc);

        // 密码
        gbc.gridx = 0; gbc.gridy = 2;
        inputPanel.add(new JLabel("密码:"), gbc);
        gbc.gridx = 1;
        passwordField = new JPasswordField(15);
        inputPanel.add(passwordField, gbc);

        // 按钮面板（单独放在SOUTH区域）
        JPanel buttonPanel = new JPanel();
        loginButton = new JButton("登录");
        registerButton = new JButton("注册");
        buttonPanel.add(loginButton);
        buttonPanel.add(registerButton);

        // 将输入面板和按钮面板添加到主窗口
        add(inputPanel, BorderLayout.CENTER);
        add(buttonPanel, BorderLayout.SOUTH);
    }

    // Getters和其他方法保持不变
    public JButton getRegisterButton() { return registerButton; }
    public void addLoginListener(ActionListener listener) {
        loginButton.addActionListener(listener);
    }
    public String getUsername() { return usernameField.getText(); }
    public String getPassword() { return new String(passwordField.getPassword()); }
    public boolean isAdmin() { return roleComboBox.getSelectedItem().equals("管理员"); }
    public void showError(String message) {
        JOptionPane.showMessageDialog(this, message, "登录错误", JOptionPane.ERROR_MESSAGE);
    }
}